﻿using System;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.DataCheck.model
{
    /// <summary>
    /// 等高特征线高程属性检查（检查等高特征线的高程值是否精确到0.1米）
    /// </summary>
    public class TerFeatLnEleAttrCheck : Check
    {
        public int Check(string errType, IFeatureLayer pFeatLyr, IGeometry pGeometry, string whereClause, string fcodename, string elevationname)
        {
            IFeatureClass pFeatCls = pFeatLyr.FeatureClass;
            if (pFeatCls.ShapeType != esriGeometryType.esriGeometryPolyline)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层不是线状图层，检查失败！");
                return -1;
            }

            int idxfcode = pFeatCls.FindField(fcodename);
            if (idxfcode == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到分类代码[" + fcodename + "]字段，检查失败！");
                return -1;
            }

            int idxelevation = pFeatCls.FindField(elevationname);
            if (idxelevation == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到高程[" + elevationname + "]字段，检查失败！");
                return -1;
            }

            ISpatialFilter pSpatialFileter = new SpatialFilterClass();
            if (pGeometry != null) pSpatialFileter.Geometry = pGeometry;

            string sql = fcodename + " in('770500376')";
            pSpatialFileter.WhereClause = whereClause == "" ? sql : whereClause + " and " + sql;

            pSpatialFileter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            pSpatialFileter.GeometryField = pFeatCls.ShapeFieldName;

            showFakePro(true);
            LogHelper.WriteLog("正在计算需要检查的要素数目...");

            int max = FeatureClassHelper.GetFeatureCountFromLayer(pFeatLyr, pSpatialFileter);// pFeatCls.FeatureCount(pSpatialFileter);

            LogHelper.WriteLog("计算完毕。共有" + max + "个要素参与检查。");
            showFakePro(false);

            if (max == 0)
            {
                LogHelper.WriteLog("没有满足检查要求的要素存在。");
                return -1;
            }

            setProcessBarMax(max);

            IFeatureCursor pFeatCur;
            pFeatCur = pFeatLyr.Search(pSpatialFileter, false);

            IFeature pFeature;
            int count = 0;
            int errCount = 0;

            while ((pFeature = pFeatCur.NextFeature()) != null)
            {
                if (IsStop()) break;

                setProcessBarCurrent(count);
                count++;

                if (pFeature.get_Value(idxelevation) == DBNull.Value || pFeature.get_Value(idxelevation) == null)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "高程值为空");
                    errCount++;
                    continue;
                }

                double elevationvalue = Double.Parse(pFeature.get_Value(idxelevation).ToString());

                if (elevationvalue <= 0)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "高程值为非正数");
                    errCount++;
                    continue;
                }

                if ((elevationvalue * 10).ToString ().IndexOf ('.')!=-1)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "高程值不是0.1的整数倍");
                    errCount++;
                    continue;
                }

            }

            finishPro.Invoke();

            DataCheckUtility.ReleaseComObject(pFeature);
            DataCheckUtility.ReleaseComObject(pFeatCur);

            LogHelper.WriteLog("等高特征线线高程属性异常检查结束。共发现" + errCount + "处错误！\n");

            return errCount;
        }

    }
}
